package com.diet263.l.testlearn

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.Gravity
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    private var TAG = this::class.java.simpleName

    var popW: PopupWindow? = null
    var initX = 0
    var initY = 0
    var cView: View? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        cView = View.inflate(this, R.layout.activity_main, null)
        setContentView(cView)
        tv_note.setOnClickListener { Log.e(TAG, "AC setOnClickListener ") }
        tv_note.setOnTouchListener(View.OnTouchListener { _, e ->
            Log.e(TAG, "AC tv_note onTouch Id " + e.action + " ---- " + e.source)
            return@OnTouchListener true
        })
        tv_B.setOnClickListener { testPopupWindow() }
        tv_B.setOnTouchListener(View.OnTouchListener { _, e ->
            Log.e(TAG, "AC tv_B onTouch action Id " + e.action + " ---- " + e.source)
            return@OnTouchListener false
        })
        val popView = View.inflate(this, R.layout.layout_popup_view, null);
        val width = ViewGroup.LayoutParams.WRAP_CONTENT
        val height = ViewGroup.LayoutParams.WRAP_CONTENT
        popW = PopupWindow(popView, width, height)

    }

    private fun testPopupWindow() {
//        popW?.update()
//        popW?.showAsDropDown(tv_note)
        Log.e(TAG, "AC B setOnClickListener ")
    }

    override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
        Log.e(TAG, "AC dispatchTouchEvent " + ev?.action)
        return super.dispatchTouchEvent(ev)
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        Log.e(TAG, "AC dispatchTouchEvent ${event?.action}")
        event?.let {
            Log.e(TAG, "AC x ${event?.x.toInt()} y ${event?.y.toInt()}")
            Log.e(TAG, "AC initX $initX initY $initY")
            when (it.action) {
                MotionEvent.ACTION_DOWN -> {
                    initX = event.x.toInt()
                    initY = event?.y.toInt()
                    popW?.showAsDropDown(tv_B)
                    return true
                }
                MotionEvent.ACTION_MOVE -> {
                    //tv_note.text = getPosXY(event)
                    popW?.update(
                        tv_B, event.x.toInt() - initX, event?.y.toInt(),
                        ViewGroup.LayoutParams.WRAP_CONTENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT
                    )
                    return true
                }
                MotionEvent.ACTION_UP -> {
                    //tv_note.text = getPosXY(event)
                    popW?.dismiss()
                    return true
                }
                else -> return super.onTouchEvent(event)
            }
        }
        return super.onTouchEvent(event)
    }

    private fun getPosXY(event: MotionEvent) = "Pos : x " + event.x.toInt() + " y " + event.y.toInt()


}
